package inet.sms.network;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import red.sms.util.IConstant;

public class TelcoConfig {

	/**
	 * 
	 */
	public static Properties props = new Properties();
	public static final String fileName = "conf\\telco.conf";
	public static String viettel_prefix;
	public static String vinaphone_prefix;
	public static String mobifone_prefix;
	public static void main(String[] args) {
		//NumberUtil test = new NumberUtil();
		//System.out.println(test.getTelcoType("84912456890"));
		//System.out.println(test.getPrefixPhoneNumber("8416232225267"));
	}
	
	/**
	 * @author SONPH
	 * 
	 * @param phoneNumber
	 * @return Telcotype of a phoneNumber
	 * @date 12 Oct 2013
	 */
	
	public static String getTelcoType(String phoneNumber){
		try {
			props.load(new FileInputStream(fileName));
			viettel_prefix = props.getProperty("viettel_prefix", viettel_prefix);
			mobifone_prefix = props.getProperty("mobifone_prefix", viettel_prefix);
			vinaphone_prefix = props.getProperty("vinaphone_prefix", viettel_prefix);
			
			String prefixPhoneNumber = TelcoConfig.getPrefixPhoneNumber(phoneNumber);
			if (viettel_prefix.contains(prefixPhoneNumber)) return IConstant.TELCO.VIETTEL;
			if (mobifone_prefix.contains(prefixPhoneNumber)) return IConstant.TELCO.MOBIFONE;
			if (vinaphone_prefix.contains(prefixPhoneNumber)) return IConstant.TELCO.VINAPHONE;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		
		return "";
	}
	
	/**
	 * @author SONPH
	 * 
	 * @param phoneNumber
	 * @return prefix of a phoneNumber
	 * @date 12 Oct 2013
	 */
	
	public static String getPrefixPhoneNumber(String phoneNumber){
		if ((phoneNumber == null) || (phoneNumber.equals(""))){
			return "";
		}else{
			if (phoneNumber.startsWith("09")){
				return phoneNumber.substring(1, 3);
			}else if(phoneNumber.startsWith("849")){
				return phoneNumber.substring(2,4);
			}else if(phoneNumber.startsWith("01")){
				return phoneNumber.substring(1,4);
			}else{ //startwith "841"
				return phoneNumber.substring(2,5);
			}
		}
	}

}
